// 
//  CommandPattern.cs
//  
//  Author:
//       Ray Shaw <demogodyou@gmail.com>
// 
//  Copyright (c) 2009 Ray Shaw(xiaolei)
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:2.0.50727.3053
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;
using System.Collections;


namespace SharpNote.Core.Command
{
	public abstract class CommandBase<TResult, TParam>
	{
		public CommandBase ()
		{
		}

		public abstract void Execute (ref TResult result, params TParam[] parameters);
		public abstract string GetHelpeText ();
		public abstract string GetAboutText ();
	}

	public abstract class CommandBase
	{
		public CommandBase ()
		{
		}

		public abstract void Execute (ref object result, params object[] parameters);
	}

	public class Invoker<TResult, TParam>
	{
		private CommandBase<TResult, TParam> _command;
		public void SetCommand (CommandBase<TResult, TParam> cmd)
		{
			_command = cmd;
		}

		public void ExecuteCommand (ref TResult result, params TParam[] parameters)
		{
			if (_command != null) {
				_command.Execute (ref result, parameters);
			}
		}
	}

	public class Invoker
	{
		private CommandBase _command;
		public void SetCommand (CommandBase cmd)
		{
			_command = cmd;
		}

		public void ExecuteCommand (ref object result, params object[] parameters)
		{
			if (_command != null) {
				_command.Execute (ref result, parameters);
			}
		}
	}

	public class CommandFactory
	{
		private ArrayList _allCommandTypes = new ArrayList();

		public CommandFactory ()
		{
			Type[] types = Assembly.GetExecutingAssembly ().GetTypes ();
			foreach (Type type in types) {
				if (type.BaseType == typeof(CommandBase)) {
					_allCommandTypes.Add (type);
				}
			}
		}


		public CommandBase GetCommand (string commandName)
		{
			Type matchedType = null;

			if (_allCommandTypes == null) {
				return null;
			}

			foreach (object obj in _allCommandTypes) {
				Type type = obj as Type;
				object[] attributes = type.GetCustomAttributes (true);
				if (attributes != null && attributes.Length > 0) {
					CommandAttribute att = attributes[0] as CommandAttribute;
					if (att != null) {
						if (att.CommandFullName.Equals (commandName, StringComparison.CurrentCultureIgnoreCase) || (att.CommandAlias.IndexOf (commandName, StringComparison.CurrentCultureIgnoreCase) != -1)) {
							matchedType = type;
							break;
						}
					}
				}
			}

			if (matchedType != null) {
				return Activator.CreateInstance (matchedType) as CommandBase;
			} else {
				return null;
			}
		}
	}
}
